﻿using Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using Utility;

namespace Cell_Simulate
{
    /// <summary>
    /// winprop_simulate 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务，请取消注释以下行。 
    [System.Web.Script.Services.ScriptService]
    public class winprop_simulate : System.Web.Services.WebService
    {
        /**/
        [WebMethod]
        public void single_cell_simulate(int cityCode, double cell_latitude, double cell_longitude, double ant_height, double power, double frequency, int azimuth, double downtilt, string id)
        {
            var result = new { Status = 0, Msg = "success" };
            NLog.Logger _logger = NLog.LogManager.GetCurrentClassLogger();
            try
            {

                System.Diagnostics.Process process = new System.Diagnostics.Process();

                process.StartInfo.WorkingDirectory = AppDomain.CurrentDomain.BaseDirectory;  //应用程序所在目录
                //process.StartInfo.UseShellExecute = false;
                //process.StartInfo.RedirectStandardOutput = true;
                //process.StartInfo.CreateNoWindow = true;

                process.StartInfo.FileName = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "bin", "ConsoleApp1.exe");  //执行程序完整路径
                process.StartInfo.UseShellExecute = true;
                process.StartInfo.CreateNoWindow = false;
                process.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal;
                //process.StartInfo.RedirectStandardOutput = true;
                process.StartInfo.Verb = "runas";

                process.Start();

                process.WaitForExit();

                //winprop.wrapper wrapper = new winprop.wrapper();

                //string odaFilePath = getODAByCity(cityCode);
                //_logger.Debug(odaFilePath);
                //wrapper.load_building(odaFilePath);

                //string outputFolder = System.IO.Path.Combine(System.Configuration.ConfigurationManager.AppSettings["outputfolder"], id);
                //double mx = 0, my = 0;
                //MecatorUtil.loc_to_mercator(cell_longitude, cell_latitude, ref mx, ref my);
                //if (downtilt > 0)
                //{
                //    downtilt = downtilt - 6;
                //}
                //winprop.Freq freq = new winprop.Freq();
                //_logger.Debug("start");
                //int error = wrapper.single_cell_winprop(outputFolder, cityCode, 5, 500, 20, new Res_Cell()
                //{
                //    AntHeight = ant_height,
                //    CellLatitude = my,
                //    CellLongitude = mx,
                //    CellName = id,
                //    Frequency = freq.GetFreqByCenterChannelNumber(frequency.ToString()),
                //    HorizonAngleToEast = azimuth,
                //    VerticalAngleToHorizontal = downtilt,
                //    RSTxPower = power
                //});
                //_logger.Debug("end");
                //wrapper.release_building();
                result = new { Status = 0, Msg = "" };
            }
            catch (Exception ex)
            {
                result = new { Status = 1, Msg = ex.Message };
            }
            finally {
                HttpContext.Current.Response.Write(Newtonsoft.Json.JsonConvert.SerializeObject(result));
                HttpContext.Current.Response.End();
            }
        }

        private string getODAByCity(int cityCode)
        {
            
            return System.IO.Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "oda", cityCode.ToString() + ".oda");
        }
    }
}
